/**
 * useStoreState description
 * @namespace useStoreState
 * @param  {string} stateName 	the stateName to use
 * @param  {function} dispatch  the dispatch to call
 * @param  {value} resetValue   the init value to reset
 * @return {stateValue}         the value of the stateName
 * @example
 	const name1 = useStoreState("userName");
 */
export default function useStoreState(stateName, dispatch, resetValue) {
	if (resetValue) {
		StateApi.onlySetStateValue(stateName, resetValue);
	}
	const initStateValue = resetValue || StateApi.getStateValue(stateName);
	const [stateValue, setStateValue] = React.useState(initStateValue);

	React.useEffect(() => {
		function updateState(newData) {
			setStateValue(function(data) {
				return newData;
			});
		}
		// UserApi.addUserStateHook(setStateValue);
		let clean = StateApi.addStoreStateHook(
			stateName,
			updateState,
			dispatch
		);
		return function() {
			// console.log("remove");
			clean();
		};
	});

	return stateValue;
}
